package cn.highedu.component;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

@Component
public class MyCustomHealthCheck implements HealthIndicator {

    @Autowired
    private DataSource dataSource;

    @Override
    public Health health() {
        // 检查数据库连接是否正常
        String SQL = "SELECT 1";
        try {
            dataSource.getConnection().createStatement().execute(SQL);
            return Health.up().build();
        } catch (Exception e) {
            e.printStackTrace();
            // return Health.down().withDetail("数据库连接失败", e.getMessage()).build();
            return Health.status("数据库连接失败").withDetail("error", e.getMessage()).build();
        }
    }
}
